#include "projectile2.h"

// constructor
Projectile2::Projectile2(Vector position, Vector direction, GLfloat velocity, GLfloat length, double time)
{
	this->position = position;
	this->direction = direction;
	this->velocity = velocity;
	this->length = length;
	this->time = time;

	tracer.start = position;
	tracer.end = position;

	GLfloat xr = (direction.x / 180 * 3.141592654f);
	GLfloat yr = (direction.y / 180 * 3.141592654f);

	GLfloat ht = (GLfloat)cos(xr) * length;
	tracer.end.y -= (GLfloat)sin(xr) * length;
	tracer.end.x += (GLfloat)sin(yr) * ht;
	tracer.end.z -= (GLfloat)cos(yr) * ht;

	/*scanLine.start = p;
	scanLine.end = p;
	GLfloat hr = (GLfloat)cos(xr) * 100.0f;
	scanLine.end.y -= (GLfloat)sin(xr) * 100.0f;
	scanLine.end.x += (GLfloat)sin(yr) * ht;
	scanLine.end.z -= (GLfloat)cos(yr) * ht;*/
}

GLfloat Projectile2::getVelocity()
{
	return velocity;
}

bool Projectile2::isActive()
{
	return ((glfwGetTime() - time) < 10.0);
}

void Projectile2::setVelocity(GLfloat velocity)
{
	this->velocity = velocity;
}

void Projectile2::draw()
{
	tracer.draw();	
}

void Projectile2::update(double deltaTime)
{
	if(velocity != 0)
	{
		GLfloat vx = (GLfloat)(velocity * deltaTime);

		GLfloat xr = (direction.x / 180 * Math::pi);
		GLfloat yr = (direction.y / 180 * Math::pi);
		GLfloat h = (GLfloat)cos(xr) * vx;
	
		tracer.start.y -= (GLfloat)sin(xr) * vx;
		tracer.start.x += (GLfloat)sin(yr) * h;
		tracer.start.z -= (GLfloat)cos(yr) * h;

		tracer.end.y -= (GLfloat)sin(xr) * vx;
		tracer.end.x += (GLfloat)sin(yr) * h;	
		tracer.end.z -= (GLfloat)cos(yr) * h;	
	}
}

void Projectile2::react(Vector intersect)
{
	velocity = 0;

	tracer.start = tracer.start - (tracer.end - intersect);
	tracer.end = intersect;
}